import sun.invoke.empty.Empty;

import java.util.Arrays;

class MyArrayList{
    public int[] elem;
    public int usedSize;

    public MyArrayList(){
        this.elem = new int[10];
    }

    public void diaplay(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i] + " ");
        }
        System.out.println();
    }

    public void add(int pos, int data){
        if(pos < 0 || pos > usedSize){
            System.out.println("位置不合法");
            return;
        }
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        for (int i = this.usedSize-1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
    public boolean isFull(){
        return this.usedSize == this.elem.length;
    }
    //判定一个数字是否包含
    public boolean contains(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    public int search(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    //获取pos位置
    public int getPos(int pos){
        if(pos < 0 || pos >= this.usedSize){
            System.out.println("pos位置不合法");
            return -1;
        }
        if(isEmpty()){
            System.out.println("顺序表为空");
            return -1;
        }
        return this.elem[pos];
    }
    public boolean isEmpty(){
        return this.usedSize == 0;
    }
//更新pos的数值
    public void setPos(int pos, int value){
        if(pos < 0 || pos >= this.usedSize){
            System.out.println("pos位置不合法");
            return;
        }
        this.elem[pos] = value;

    }

    //删除第一次出现的关键字key
    //1.看第一次是不是空的
    //2.查找你要删除的数字toRemove
    //3.elem[i] = elem[i+1]; i < usedSize - 1;
    public void remove(int toRemove){
        if(isEmpty()) {
            System.out.println("顺序表为空");
            return;
        }
        int index = search(toRemove);
        if(index == -1){
            System.out.println("没有你要删除的数字");
            return;
        }
        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }
    //清空顺序表
    public void clear(){
        this.usedSize = 0;
        //for (int i = 0; i < usedSize - 1; i++) {
            //this.elem[i] = null;
       // }

    }
}


public class TestDemo {
    public static void main(String[] args){
        MyArrayList myarraylist = new MyArrayList();
        myarraylist.add(0,1);
        myarraylist.add(1,2);
        myarraylist.add(2,3);
        myarraylist.add(3,4);
        myarraylist.diaplay();
        myarraylist.setPos(1,99);
        myarraylist.diaplay();
//        System.out.println(myarraylist.contains(3));
//        System.out.println(myarraylist.getPos(3));

    }
}
/**
 * 1.插入和删除顺序表必须得移动元素
 * 2.扩容也是问题
 * 3.想放第十一个 扩容两倍 20 但是你只放了十一个九个没放浪费了
 */